package com.data_structure_algorithm.data_structure.hash;

/**
 * @Authror PABLO
 * @Date 2022/3/28 15:23
 * @Desc 哈希表的某个下标，每个下标对应一个emp头节点
 */
public class EmpLinkedList {
    //当前链表对应的下标
    int index;

    //头节点
    Emp head = new Emp(0, "head");


    public void add(Emp emp) {
        Emp temp = head;
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (temp.next.id > emp.id) {
                break;
            }
            temp = temp.next;
        }

        temp.next = emp;
        emp.next = temp.next.next;
    }

    public void update(Emp emp) {
        Emp temp = head;

        while (true) {
            if (temp.next.id == emp.id) {
                break;
            }
            temp = temp.next;
        }

        temp.next.name = emp.name;
    }


    public void remove(Emp emp) {
        Emp temp = head;

        while (true) {
            if (temp.next.id == emp.id) {
                break;
            }
            temp = temp.next;
        }

        temp.next = temp.next.next;

    }

    public void find(int id) {
        Emp temp = head;

        boolean flag = false;
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (temp.next.id == id) {
                flag = true;
                break;
            }
            temp = temp.next;
        }

        System.out.println();
        if (flag) {
            System.out.println("根据id找到的员工" + temp.next);
        } else {
            System.out.println("未找到该员工");
        }

    }

    public void print() {

        System.out.print("对应哈希表下标为【" + this.index + "】  ");
        Emp temp = head;
        while (temp.next != null) {
            System.out.print(temp.next.toString() + "--->");
            temp = temp.next;
        }
    }
}
